const express = require('express');
let router = express.Router();
let Db = require('../utils/db完整');
const apiUrl = require("../config/config")
let {api_url}=apiUrl;
//online head四行选项
router.get('/areas', async (req, res, next) => {
  let sql = `SELECT id,area_name FROM e_area`;
  let resData = await Db(sql);
  let [err, results] = resData;
  console.log(err, 111); console.log(results, 222);
  if (err) {
    next([err, '请求数据失败']);
    return;
  };
  if (results) {
    res.send({
      status: 200,
      msg: 'areas请求成功',
      results
    });
  };
});

router.get('/grade', async (req, res, next) => {
  let sql = `SELECT id,grade_name FROM e_grade`;
  let resData = await Db(sql);
  let [err, results] = resData;
  console.log(err, 111); console.log(results, 222);
  if (err) {
    next([err, '请求数据失败']);
    return;
  };
  if (results) {
    res.send({
      status: 200,
      msg: 'areas请求成功',
      results
    });
  };
})

router.get('/subject', async (req, res, next) => {
  let sql = `SELECT id,subject_name FROM e_subject`;
  let resData = await Db(sql);
  let [err, results] = resData;
  console.log(err, 111); console.log(results, 222);
  if (err) {
    next([err, '请求数据失败']);
    return;
  };
  if (results) {
    res.send({
      status: 200,
      msg: 'areas请求成功',
      results
    });
  };
});

router.get('/category', async (req, res, next) => {
  let sql = `SELECT id,category_name FROM e_category`;
  let resData = await Db(sql);
  let [err, results] = resData;
  console.log(err, 111); console.log(results, 222);
  if (err) {
    next([err, '请求数据失败']);
    return;
  };
  if (results) {
    res.send({
      status: 200,
      msg: 'areas请求成功',
      results
    });
  };
})



//分页请求
router.get('/course', async (req, res, next) => {
  let { page, size,
    area_name,
    grade_name,
    category_name,
    subject_name } = req.query
    console.log(req.query,'get接收参数');
    
  //分类传参：有没有传参 传了几个参数
  /*法1 先选出status!=4的课程，后面将条件字符串拼接起来,最后放到where条件后面 */
  let str ='';
  if (area_name) {
    str += ` and area_name='${area_name}'`
  };
  if (grade_name) {
    str += ` and grade_name='${grade_name}'`
  };
  if (subject_name) {
    str += ` and subject_name='${subject_name}'`
  };
  if (category_name) {
    str += ` and category_name='${category_name}'`
  };


  //请求总页码
  let sqlA = `SELECT id,cid,title,
  area_name,
  grade_name,
  category_name,
  CONCAT('${api_url}','/uploads/','online/',image_src) AS image_src,
  subject_name FROM  e_course
  WHERE status!=4 ${str}`;
  let resDataA = await Db(sqlA);
  let [errA, resultsA] = resDataA;
  if (errA) {
    next([errA, '请求数据失败']);
    return;
  };
  let rows = resultsA.length
  console.log(rows, '总条数');
  let pages = Math.ceil(rows / size)
  console.log(pages, '总页数');


  //请求分页

  /*法2，假如没有status先做判断.需要做where和and判断使用*/
  // let str='';
  // if(area_name){
  //   if(str==''){
  //     str+=` where area_name=${area_name}`
  //   }else{
  //     str+=` and area_name=${area_name}`
  //   }
  // };
  // ...
  // if(category_name){
  //   if(str==''){
  //     str+=` where category_name=${area_name}`
  //   }else{
  //     str+=` and category_name=${category_name}`
  //   }
  // };
  console.log(area_name,str,'字符串');

  console.log(page, 'page');

  let sql = `SELECT id,cid,title,price,
  area_name,
  grade_name,
  category_name,
  CONCAT('${api_url}','/uploads/','online/',image_src) AS image_src,
  subject_name FROM  e_course
  WHERE status!=4 ${str}
  LIMIT ${size * (page * 1 - 1)},${size}`;
  let resData = await Db(sql);
  let [err, results] = resData;
  console.log(err, '数据库错误'); 
  console.log(results.length, '当前页个数');
  if (err) {
    next([err, '请求数据失败']);
    return;
  };
  if (results) {
    res.send({
      status: 200,
      msg: 'course请求成功',
      results: {
        data: results?results:[],
        page: page * 1,
        pages,
        rows
      }
    });
  };
})
module.exports = router;